<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/2/27
 * Time: 10:30
 */

namespace app\api\controller\v9;

use app\api\controller\v2\Auth;
use app\common\cachemodel\AppUserReply;
use app\common\cachemodel\ArticleCommentGood;
use app\common\cachemodel\ArticleCommentReply;
use app\common\model\MessageCenter as MessageCenterModel;
use think\Cache;

/**
 * 消息中心相关接口
 * @version 1.7.0
 * Class MessageCenter
 * @package app\api\controller\v9
 */
class MessageCenter extends Auth
{
    /**
     * 一键已读消息相关接口
     * @return \think\response\Json
     * @throws \Exception
     */
    public function all_read_message()
    {
        $param['type'] = input('type/d', 0);
        $validate = validate('MessageCenter');
        if ($validate->scene('all_read_message')->check($param) !== true)
        {
            return show(config('code.error'), (string)$validate->getError());
        }
        $res = $this->allReadMsg($param['type']);
        if ($res)
        {
            return show(config('code.success'), '操作成功');
        }
        return show(config('code.error'), '操作失败');
    }

    /**
     * 多选已读用户消息消息相关接口
     * @return \think\response\Json
     * @throws \Exception
     */
    public function read_message()
    {
        $param['type'] = input('type/d', 0);
        $param['ids'] = input('ids/s', '');
        if ($param['ids'])
        {
            $ids = explode(',', $param['ids']);//消息id
        } else
        {
            $ids = [];
        }

        //0=>全部 1=>系统消息, 2=>服务消息 3=>点赞 4=>评论
        switch ($param['type'])
        {
            case 1://系统已读
            case 2://服务已读
                if ($ids)
                {
                    $validate = validate('MessageCenter');
                    if ($validate->scene('read_message')->check($param) !== true)
                    {
                        return show(config('code.error'), (string)$validate->getError());
                    }
                    $type = $param['type'];//消息类型【1=》系统，2=》服务】
                    $MsgModel = new MessageCenterModel();
                    $res = $MsgModel->UpdateData($this->user['id'], $type, $ids, ['is_read' => 1]);
                    if ($res)
                    {
                        return show(config('code.success'), '操作成功');
                    }
                } else
                {
                    //全部已读
                    $res = $this->allReadMsg();
                    if ($res)
                    {
                        return show(config('code.success'), '操作成功');
                    }
                    return show(config('code.error'), '操作失败');
                }

                break;
            case 3://点赞已读
                Cache::rm('user_good_unread_status_' . $this->user['id']);
                break;
            case 4://评论已读
                if ($ids)
                {
                    (new AppUserReply())->replyRead($this->user['id'], $ids);
                } else
                {
                    //全部已读
                    $replyModel = new ArticleCommentReply();
                    $ids = $replyModel->getUserReplyMeFromCache($this->user['id']);
                    $read_ids = $replyModel->getUserReadReply($this->user['id']);
                    //获取用户未读数
                    $unread_ids = array_diff($ids, $read_ids);
                    (new AppUserReply())->replyRead($this->user['id'], $unread_ids);
                }
                break;
            default://全部已读
                //消息全部已读
                $this->allReadMsg();
                //点赞全部已读
                Cache::rm('user_good_unread_status_' . $this->user['id']);
                //回复全部已读
                $replyModel = new ArticleCommentReply();
                $ids = $replyModel->getUserReplyMeFromCache($this->user['id']);
                $read_ids = $replyModel->getUserReadReply($this->user['id']);
                //获取用户未读数
                $unread_ids = array_diff($ids, $read_ids);
                (new AppUserReply())->replyRead($this->user['id'], $unread_ids);
                break;
        }
        return show(config('code.success'), '操作成功', []);
    }

    /**
     * 多选删除用户消息消息相关接口
     * @return \think\response\Json
     * @throws \Exception
     */
    public function del_message()
    {
        $param['type'] = input('type/d', 0);
        $param['ids'] = input('ids/s', '');
        $ids = explode(',', $param['ids']);
        //1=>系统消息, 2=>服务消息 3=>点赞
        switch ($param['type'])
        {
            case 1://系统删除
            case 2://服务删除
                $validate = validate('MessageCenter');
                if ($validate->scene('del_message')->check($param) !== true)
                {
                    return show(config('code.error'), (string)$validate->getError());
                }
                $type = $param['type'];//消息类型【1=》系统，2=》服务】
                $msgModel = new MessageCenterModel();
                $res = $msgModel->DelData($this->user['id'], $type, $ids, ['status' => 1, 'is_read' => 1]);
                if ($res)
                {
                    return show(config('code.success'), '操作成功');
                }
                break;
            case 3://点赞删除
                (new ArticleCommentGood())->updateData($ids);
                break;
        }
        return show(config('code.success'), '删除成功', []);
    }

    /**
     * 消息全部已读
     * @param int $type
     * @return bool
     */
    protected function allReadMsg($type = 0)
    {
        try
        {
            $uid = $this->user['id'];
            $msgModel = new MessageCenterModel();
            if ($type)
            {
                //获取当前用户所有未读消息
                if (isset($msgModel))
                {
                    $mids = $msgModel->getAllUserMessage(['uid' => $uid, 'type' => $type]);
                } else
                {
                    $mids = [];
                }
                $msgModel->UpdateData($this->user['id'], 1, $mids, ['is_read' => 1]);
            } else
            {
                //获取当前用户所有未读消息
                if (isset($msgModel))
                {
                    $mids1 = $msgModel->getAllUserMessage(['uid' => $uid, 'type' => 1]);
                    $mids2 = $msgModel->getAllUserMessage(['uid' => $uid, 'type' => 2]);
                } else
                {
                    $mids1 = [];
                    $mids2 = [];
                }
                $msgModel->UpdateData($this->user['id'], 1, $mids1, ['is_read' => 1]);
                $msgModel->UpdateData($this->user['id'], 2, $mids2, ['is_read' => 1]);
            }
            return true;
        }
        catch (\Exception $e)
        {
            return false;
        }
    }
}
